<?php 
/*
 module:		小程序设置
 create_time:	2022-06-20 19:12:51
 author:		
 contact:		
*/

namespace app\backend\controller;

use app\backend\service\SysSiteService;
use app\backend\model\SysSite as SysSiteModel;
use think\facade\Db;

class SysSite extends Admin {
    
    
    /*首页数据列表*/
    function index(){
        if (!$this->request->isAjax()){
            $infos = SysSiteModel::where(['wxapp_id'=> session('backend.wxapp_id')])->find();
            $this->view->assign('infos',$infos);
            return view('index');
        }else{
            $limit  = $this->request->post('limit', 20, 'intval');
            $offset = $this->request->post('offset', 0, 'intval');
            $page   = floor($offset / $limit) +1 ;
            
            $where = [];
            $where['wxapp_id'] = session('backend.wxapp_id');
            $order  = $this->request->post('order', '', 'serach_in');	//排序字段 bootstrap-table 传入
            $sort  = $this->request->post('sort', '', 'serach_in');		//排序方式 desc 或 asc
            
            $field = 'id,app_id,app_secret,mch_id';
            $orderby = ($sort && $order) ? $sort.' '.$order : 'id desc';
            
            $res = SysSiteService::indexList(formatWhere($where),$field,$orderby,$limit,$page);
            return json($res);
        }
    }
    
    
    
    /*添加*/
    function add(){
        if (!$this->request->isPost()){
            return view('add');
        }else{
            $postField = 'yxzy,dzqy,xzcyl,app_id,wxapp_id,app_secret,mch_id,mch_key,cert_path,key_path,create_time,about_us,proportion,agreement,hn_agreement,currency_agreement,superior_take_prop,matchmaker_take_prop,platform_take_prop,no_vip_frequency,min_withdrawal,short_message,short_message_key,short_message_account,withdrawal_btn,tel,gift_template_id,chat_template_id,official_account_appid,official_account_appsecre,valid_profile,zs_agreement';
            $data = $this->request->only(explode(',',$postField),'post',null);
            $res = SysSiteService::add($data);
            return json(['status'=>'00','msg'=>'添加成功']);
        }
    }
    
    
    
    /*修改*/
    function update(){
        if (!$this->request->isPost()){
            $id = $this->request->get('id','','serach_in');
            if(!$id) $this->error('参数错误');
            $this->view->assign('info',checkData(SysSiteModel::find($id)));
            return view('update');
        }else{
            $postField = 'yxzy,dzqy,xzcyl,id,app_id,wxapp_id,app_secret,mch_id,mch_key,cert_path,key_path,create_time,about_us,proportion,agreement,hn_agreement,zs_agreement,currency_agreement,user_agreement,superior_take_prop,matchmaker_take_prop,platform_take_prop,no_vip_frequency,min_withdrawal,short_message,short_message_key,short_message_account,withdrawal_btn,tel,gift_template_id,chat_template_id,official_account_appid,official_account_appsecret,app_app_id,valid_profile';
            $data = $this->request->only(explode(',',$postField),'post',null);
            $res = SysSiteService::update($data);
            return json(['status'=>'00','msg'=>'修改成功']);
        }
    }
    


}

